﻿using Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using xjjxmm.infrastructure.Authorizations;
using xjjxmm.infrastructure.repository.impl.sugar;
using xjjxmm.infrastructure.repository.interfaces;
using xjjxmm.infrastructure.Webs;
using xjjxmm.infrastructure.xjjxmmLog.LogKit;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using XjjXmm.Infrastructure.Jwt;
using XjjXmm.Infrastructure.User;
using XjjXmm.Infrastructure.Webs.ActionResult;

namespace xjjxmm.infrastructure.compont;

public class AutofacCompont : ICompont
{
    public int Order { get; set; } = 100;

    public bool Regist(WebApplicationBuilder builder)
    {
        //引入autofac
        builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
        return false;
    }


    public bool Regist(ContainerBuilder builder2)
    {
        builder2.RegisterGeneric(typeof(XjjXmmSeriLogger<>)).As(typeof(ILog<>)).SingleInstance();

        builder2.RegisterType<LoggerInterceptor>().AsSelf().SingleInstance();
        builder2.RegisterType<LoggerAsyncInterceptor>().AsSelf().SingleInstance();

        builder2.RegisterGeneric(typeof(Repository<>))
            .As(typeof(IRepository<>))
            .SingleInstance();

        //builder2.RegisterGeneric(typeof(MongoRepository<>)).InstancePerLifetimeScope();

        builder2.RegisterType<XjjXmm.Infrastructure.User.TokenContext>().SingleInstance();

        builder2.RegisterType<UserThreadContext>().As<IUserThreadContext>().InstancePerLifetimeScope();

        builder2.RegisterType<ThreadLocalContext>().SingleInstance();
        builder2.RegisterType<UserContextAccessor>().As<IUserContextAccessor>().SingleInstance();

        builder2.RegisterType<SqlContext>().As<ISqlContext>().SingleInstance();

        builder2.RegisterType<StreamResultExecutor>().As<IActionResultExecutor<StreamResult>>().SingleInstance();

        return true;
    }
}
